/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package vista;

import java.util.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import modelo.ICandidato;
/**
 *
 * @author LEONIC
 */
public class CandidatoTableModel implements TableModel{

    private List<ICandidato> candidatos = new ArrayList<ICandidato>();
    private List<TableModelListener> listeners = new ArrayList<TableModelListener>();

    public List<ICandidato> getCandidatos() {
        return candidatos;
    }

    public void setCandidatos(List<ICandidato> candidatos) {
        this.candidatos = candidatos;
        fireContentsChanged();
    }

    public ICandidato getRow(int indexRow){
        return candidatos.get(indexRow);
    }
    public int getRowCount() {
        return candidatos.size();
    }

    public int getColumnCount() {
        return 3; //ya que tendremos tres valores a mostrar por contacto
    }

    public String getColumnName(int columnIndex) {
        String name = null;
        switch (columnIndex) {
            case 0:
                name = "dni";
                break;
            case 1:
                name = "nombre";
                break;
            case 2:
                name = "rol";
                break;
        }
        return name;
    }

    public Class<?> getColumnClass(int columnIndex) {
        return String.class; //Todos los valores de contacto son un String
    }

    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false; //No permito editar celdas
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        ICandidato prov = candidatos.get(rowIndex);
        String value = null;
        switch (columnIndex) {
            case 0:
                value = prov.getDNI();
                break;
            case 1:
                value = prov.getNombre();
                break;
            case 2:
                value = prov.getRol();
                break;
        }
        return value;

    }

    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void addTableModelListener(TableModelListener l) {
        listeners.add(l);
    }

    public void removeTableModelListener(TableModelListener l) {
        listeners.remove(l);
    }

     protected void fireContentsChanged() {
        //TableModelEvent event = new TableModelEvent(this,0,getRowCount());
         TableModelEvent event = new TableModelEvent(this, 0, this.getRowCount()-1, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT);
        for (TableModelListener listener : listeners) {
            listener.tableChanged(event);
        }
    }
}
